package com.vhans.bapi.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.vhans.core.web.controller.BaseController;
import com.vhans.core.web.model.Result;
import com.vhans.core.web.page.TableDataInfo;
import com.vhans.db.model.dto.ConditionDTO;
import com.vhans.db.model.vo.msg.MsgLastVO;
import com.vhans.db.model.vo.msg.MsgVO;
import com.vhans.db.service.ICMsgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 用户聊天信息Controller控制器
 *
 * @author vhans
 * @date 2023-05-21
 */
@Api(tags = "用户聊天信息模块")
@RestController
@RequestMapping("/msg")
public class MsgController extends BaseController {
    @Autowired
    private ICMsgService msgService;

    /**
     * 获取好友之间的最后一条聊天记录
     *
     * @param roomId 房间号
     * @return {@link MsgLastVO} 最后一条聊天记录
     */
    @SaCheckLogin
    @ApiOperation(value = "获取好友之间的最后一条聊天记录")
    @GetMapping("/getLastMsg/{roomId}")
    public Result<MsgLastVO> getLastMsg(@PathVariable("roomId") String roomId) {
        return Result.success(msgService.getLastMsg(roomId));
    }

    /**
     * 获取最近的聊天记录
     *
     * @param condition (roomId) 房间号
     * @return {@link MsgVO} 聊天记录
     */
    @SaCheckLogin
    @ApiOperation(value = "获取最近的聊天记录")
    @GetMapping("/listRecent")
    public TableDataInfo listRecent(ConditionDTO condition) {
        startPage();
        List<MsgVO> list = msgService.getRecentMsg(condition.getRoomId());
        return getDataTable(list);
    }

    /**
     * 标记消息已读
     *
     * @param roomId 房间号
     */
    @SaCheckLogin
    @ApiOperation(value = "标记消息已读")
    @GetMapping("/readMsg/{roomId}")
    public Result<?> readMsg(@PathVariable("roomId") String roomId) {
        msgService.batchReadCMsg(roomId);
        return Result.success();
    }

    /**
     * 查询用户历史聊天记录
     *
     * @param condition 条件
     * @return {@link MsgVO} 聊天记录
     */
    @SaCheckLogin
    @ApiOperation(value = "查询用户历史聊天记录")
    @GetMapping("/listHistory")
    public TableDataInfo list(ConditionDTO condition) {
        startPage();
        List<MsgVO> list = msgService.selectMsgVOList(condition);
        return getDataTable(list);
    }
}
